package org.aristotle.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.user.model.dto.PermissionDTO;
import org.aristotle.user.model.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
// 本模块
import org.aristotle.user.model.entity.RolePermission;
import org.aristotle.user.model.vo.RolePermissionVO;
import org.aristotle.user.mapper.RolePermissionMapper;
import org.aristotle.user.service.IRolePermissionService;

// 一对多(本类是多)
// 多对一(本类是多)
import org.aristotle.user.model.entity.Role;
import org.aristotle.user.service.IRoleService;
import org.aristotle.user.model.vo.RoleVO;
import org.aristotle.user.model.entity.Permission;
import org.aristotle.user.service.IPermissionService;
import org.aristotle.user.model.vo.PermissionVO;

import java.util.List;
import java.util.stream.Collectors;
// 多对多关系

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    // 多对一(本类是多)
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    // 一对多(本类是一)

    // 多对多关系中间表

    @Override
    public Page<RolePermissionVO> queryPageList(
        Long id,
        Long roleId,
        Long permissionId,
        Integer current,
        Integer size
    ) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }


            if (roleId != null) {
                queryWrapper.eq("role_id",roleId);
            }
            if (permissionId != null) {
                queryWrapper.eq("permission_id",permissionId);
            }

        Page<RolePermission> page = this.page(new Page<RolePermission>(current,size), queryWrapper);

        // 转为VO
        Page<RolePermissionVO> result = AristotleBeanUtils.transitionBean(page, Page<RolePermissionVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),RolePermissionVO::new));

        result.getRecords().forEach(rolePermissionVO -> {
            // 多对一(本类是多)
            RoleVO roleVO = this.roleService.getById(rolePermissionVO.getRoleId());
            rolePermissionVO.setRole(roleVO);
            PermissionVO permissionVO = this.permissionService.getById(rolePermissionVO.getPermissionId());
            rolePermissionVO.setPermission(permissionVO);
        });
        return result;
    }

    @Override
    public RolePermissionVO getById(Long id) {
        RolePermission rolePermission = this.baseMapper.selectById(id);
        RolePermissionVO rolePermissionVO = AristotleBeanUtils.transitionBean(rolePermission, RolePermissionVO::new);
        RoleVO roleVO = this.roleService.getById(rolePermissionVO.getRoleId());
        rolePermissionVO.setRole(roleVO);
        PermissionVO permissionVO = this.permissionService.getById(rolePermissionVO.getPermissionId());
        rolePermissionVO.setPermission(permissionVO);
        return rolePermissionVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表*/
        /*一对多(本类是一)*/

        return super.removeById(id);
    }

    // 根据外键删除
    public Boolean removeById(
        Long roleId
        ,Long permissionId
    ) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper();
        if (
            roleId == null
            && permissionId == null
        ) {
            return false;
        }
        if (roleId != null) {
            queryWrapper.eq("role_id",roleId);
        }
        if (permissionId != null) {
            queryWrapper.eq("permission_id",permissionId);
        }
        return this.remove(queryWrapper);
    }

    @Override
    public List<PermissionDTO> getPermissionDTOByRoleId(Long id) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        List<RolePermission> list = this.list(queryWrapper);

        return list.stream()
                .map(rolePermission -> this.permissionService.getDTOById(rolePermission.getPermissionId()))
                .collect(Collectors.toList());
    }

    @Override
    public List<Role> getRoleByPermissionId(Long id) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permission_id",id);
        List<RolePermission> list = this.list(queryWrapper);

        return list.stream().map(rolePermission -> this.roleService.getPoById(rolePermission.getRoleId())).collect(Collectors.toList());
    }
}
